package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.CnRegionInfoMapper;
import com.macro.mall.model.CnRegionInfo;
import com.macro.mall.service.CnRegionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CnRegionInfo的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class CnRegionInfoServiceImpl implements CnRegionInfoService {

    private boolean isDebugEnabled;

    @Autowired
    private CnRegionInfoMapper cnRegionInfoMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<CnRegionInfo> find(CnRegionInfo value) {
        //TODO这里可以做通过Assist做添加查询
        List<CnRegionInfo> result = null;
        try {
            result = cnRegionInfoMapper.selectCnRegionInfo(null);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CnRegionInfo数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public Map<String, Object> findGroupList() {
        //TODO这里可以做通过Assist做添加查询
        Assist assist1 = new Assist();
        assist1.andEq("CRI_LEVEL", "1");
        assist1.setOrder(Assist.order("CRI_SORT", true));
        Assist assist2 = new Assist();
        assist2.andEq("CRI_LEVEL", "2");
        assist2.setOrder(Assist.order("CRI_SORT", true));
        Assist assist3 = new Assist();
        assist3.andEq("CRI_LEVEL", "3");
        assist3.setOrder(Assist.order("CRI_SORT", true));
        List<CnRegionInfo> result1 = null;
        List<CnRegionInfo> result2 = null;
        List<CnRegionInfo> result3 = null;
        Map<String, Object> map = new HashMap<>();
        try {
            result1 = cnRegionInfoMapper.selectCnRegionInfo(assist1);
            result2 = cnRegionInfoMapper.selectCnRegionInfo(assist2);
            result3 = cnRegionInfoMapper.selectCnRegionInfo(assist3);
            map.put("province", result1);
            map.put("city", result2);
            map.put("area", result3);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CnRegionInfo数据集-->结果:", map);
        }
        return map;
    }

    @Override
    public List<CnRegionInfo> findGroupListById(String code, String level) {
        //TODO这里可以做通过Assist做添加查询
        Assist assist1 = new Assist();
        if (StrUtil.isNotEmpty(code)) {
            assist1.andEq("CRI_SUPERIOR_CODE", code);
        }
        if (StrUtil.isNotEmpty(level)) {
            assist1.andEq("CRI_LEVEL", level);
        }
        assist1.setOrder(Assist.order("CRI_SORT", true));
        List<CnRegionInfo> result1 = null;
        try {
            result1 = cnRegionInfoMapper.findGroupListById(assist1);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取CnRegionInfo数据集-->结果:", result1);
        }
        return result1;
    }

    @Override
    public CnRegionInfo findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CnRegionInfo的id获得CnRegionInfo对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过CnRegionInfo的id获得CnRegionInfo对象-->失败:id不能为空");
        }
        CnRegionInfo result = null;
        try {
            result = cnRegionInfoMapper.selectCnRegionInfoById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CnRegionInfo的id获得CnRegionInfo对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(CnRegionInfo value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将CnRegionInfo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            result = cnRegionInfoMapper.insertNotNullCnRegionInfo(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将CnRegionInfo中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(CnRegionInfo value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CnRegionInfo的id更新CnRegionInfo中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过CnRegionInfo的id更新CnRegionInfo中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = cnRegionInfoMapper.updateNotNullCnRegionInfoById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CnRegionInfo的id更新CnRegionInfo中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过CnRegionInfo的id删除CnRegionInfo-->失败:id不能为空");
            }
            Asserts.fail("执行通过CnRegionInfo的id删除CnRegionInfo-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = cnRegionInfoMapper.deleteCnRegionInfoById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过CnRegionInfo的id删除CnRegionInfo-->结果:", result);
        }
        return CommonResult.success(null);
    }


}